package me.gacl.demo;

import java.sql.SQLException;
import java.util.List;

import me.gacl.utils.JdbcUtils;

public class AccountDaoTest {
	
	public void add(Account account) throws SQLException {
		String sql = "insert into account(name,money) values(?,?)";
		Object[] params = {account.getName(),account.getMoney()};
		JdbcUtils.update(sql, params);
	}
	
    public void delete(int id) throws SQLException{
        String sql = "delete from account where id=?";
        Object params[] = {id};
        JdbcUtils.update(sql, params);
    }
    
    public void update(Account account) throws SQLException{
        
        String sql = "update account set name=?,money=? where id=?";
        Object params[] = {account.getName(),account.getMoney(),account.getId()};
        JdbcUtils.update(sql, params);
        
    }
    
    public Account find(int id) throws Exception{
        String sql = "select * from account where id=?";
        Object params[] = {id};
        return (Account) JdbcUtils.query(sql, params, new BeanHandler(Account.class));
    }
    
    public List<Account> getAll() throws Exception{
        String sql = "select * from account";
        Object params[] = {};
        return (List<Account>) JdbcUtils.query(sql,params,new BeanListHandler(Account.class));
    }
	
	
	public static void main(String[] args) throws Exception {
		
		AccountDaoTest accountDaoTest = new AccountDaoTest();
		
		Account account = new Account();
		account.name = "D";
		account.money = 10000;
		
		//accountDaoTest.add(account);
		
		//accountDaoTest.delete(6);
		
//		account.id = 5;
//		account.name = "A";
//		account.money = 10000;
//		accountDaoTest.update(account);
		
		Account rsaccount = (Account)accountDaoTest.find(5);
		System.out.println("id:"+rsaccount.id+"name:"+rsaccount.name+"money:"+rsaccount.money);
		
		List<Account> listRsAccount= accountDaoTest.getAll();
		for(Account rsaccount1 : listRsAccount) {
			System.out.println("id:"+rsaccount1.id+"name:"+rsaccount1.name+"money:"+rsaccount1.money);
		}
		
	}
}
